/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package menu;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/** A Menu Button
 * 
 */
public class MenuButton
{

	public enum ButtonType 
	{ 
		NONE, 
		START, NEW_GAME,
		HELP, 
		SAVE_GAME, LOAD_GAME,
		ABOUT, SETTINGS, EXIT, 
		NEXT, BACK, OK, CANCEL
	}
	
	private ButtonType type;
	private Image image, hoverImage;
	private boolean hover, down, usingNormal, shown;
	private float x, y;
 
	/** Create a new MenuButton object
	 * @param hover The hover image.
	 * @param x the x coordinate of the top-left corner (pixels).
	 * @param y the y coordinate of the top-left corner (pixels).
	 * @param type the type of button.
	 */
    public MenuButton(Image hover, float x, float y, ButtonType type)
    throws SlickException
    {

    	this.setHoverImage(hover);
    	this.setX(x);
    	this.setY(y);
    	this.setType(type);
    	this.setUsingNormal(false);
    	this.setShown(true);
    	
    }
    
    /** Create a new MenuButton object (overload).
	 * @param noraml The normal image.
	 * @param hover The hover image.
	 * @param x the x coordinate of the top-left corner (pixels).
	 * @param y the y coordinate of the top-left corner (pixels).
	 * @param type the type of button.
	 */
    public MenuButton(Image normal, Image hover, float x, float y, ButtonType type)
    throws SlickException
    {

    	this(hover,x,y,type);
    	this.setImage(normal);
    	this.setUsingNormal(true);
    	
    }
    
    /** Create a new MenuButton object (overload)
	 * @param file The name of the image file.
	 * @param x the x coordinate of the top-left corner (pixels).
	 * @param y the y coordinate of the top-left corner (pixels).
	 * @param type the type of button.
	 */
    public MenuButton(String file, float x, float y, ButtonType type)
    throws SlickException
    {
    	this(new Image(file),x,y,type);    	
    }

	/** Update the button state for a frame.
	 * @param mousex The x coordinate of the mouse cursor
	 * @param mousey The x coordinate of the mouse cursor
	 * @param mousedown The status of the mouse button being down
	 * @param delta Time passed since last frame (milliseconds).
     */
    public void update(float mousex, float mousey, boolean mousedown, int delta)
    throws SlickException
    {
    	if (this.isShown()) 
    	{
    		this.setHover(this.isOnButton(mousex, mousey));
    		this.setDown(mousedown && this.isHover());
    	}
    }

    /** Render the button
     * @param g The Slick graphics object, used for drawing.
     */
    public void render(Graphics g)
    throws SlickException
    {
    	if (this.isShown()) 
    	{
    		if (this.isHover() && !this.isDown()) 
    		{
    			this.getHoverImage().draw(this.getX(),this.getY());
    		} 
    		else if (this.isUsingNormal()) 
    		{
    			this.getImage().draw(this.getX(),this.getY());
    		}
    	}
    }
    
    /** Checks if button is being pressed.
     * @param x The x coordinate of the mouse pointer.
     * @param y The y coordinate of the mouse pointer.
     */
	public boolean isOnButton(float x, float y)
	{
		double x1 = this.getX() + this.getWidth();
		double y1 = this.getY() + this.getHeight();
		return (x >= this.getX()) &&  (x <= x1) && (y >= this.getY()) && (y <= y1);
	}
	
    /** Get the display image of the player.
     * @return The Display Image.
     */
	private Image getImage() 
	{
		return this.image;
	}

	/** Set the display image of the player.
     * @param image The Display Image.
     */
	private void setImage(Image image)
	{
		this.image = image;
	}
	
	/** Get the Height of the player.
     * @return The height.
     */
	private double getHeight() 
	{
		return this.getHoverImage().getHeight();
	}
	
	/** Get the Width of the player.
     * @return The width.
     */
	private double getWidth() 
	{
		return this.getHoverImage().getWidth();
	}
	
	/** Get the top-left x coordinate of the button.
     * @return The x coordinate.
     */
	private float getX() 
	{
		return this.x;
	}

	/** Set the top-left x coordinate of the button.
     * @param The x coordinate.
     */
	private void setX(float x)
	{
		this.x = x;
	}
	
	/** Get the top-left y coordinate of the button.
     * @return The y coordinate.
     */
	private float getY() 
	{
		return this.y;
	}

	/** Get the top-left y coordinate of the button.
     * @param The y coordinate.
     */
	private void setY(float y)
	{
		this.y = y;
	}
	
	/** Get the type of the button.
     * @return The type.
     */
	public ButtonType getType() 
	{
		return this.type;
	}

	/** Set the type of the button.
     * @param type The type of button.
     */
	private void setType(ButtonType type)
	{
		this.type = type;
	}
	
	/** Check if the mouse is hovering the button.
     * @return True if the mouse if hover, else false.
     */	
	private boolean isHover()
	{
		return this.hover;
	}

	/** Sets the mouse hover status.
     * @param hover The hover status.
     */
	private void setHover(boolean hover)
	{
		this.hover = hover;
	}
	
	/** Check if the mouse is hovering the button.
     * @return True if the mouse if hover, else false.
     */	
	public boolean isDown()
	{
		return this.down;
	}

	/** Sets the mouse down status.
     * @param hover The down status.
     */
	private void setDown(boolean down)
	{
		this.down = down;
	}

	private Image getHoverImage() 
	{
		return this.hoverImage;
	}

	private void setHoverImage(Image hoverImage)
	{
		this.hoverImage = hoverImage;
	}

	private boolean isUsingNormal() 
	{
		return this.usingNormal;
	}

	private void setUsingNormal(boolean usingNormal) 
	{
		this.usingNormal = usingNormal;
	}

	public boolean isShown() 
	{
		return this.shown;
	}

	public void setShown(boolean shown) 
	{
		this.shown = shown;
	}
	
}
